package com.dmcb.trade.business.crawlers.article;

import com.dmcb.common.business.utils.DateUtil;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 涨姿势文章解析
 * Created by Administrator on 2016/7/15.
 */
@Service
public class ZhangZiShiParser extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.ZHANGZISHI_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "h1.article-title>a";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "time.new";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "article.article-content";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "article.article-content>p img[src]";
    }

    /**
     * 获取作者选择器
     *
     * @return 作者选择器
     */
    @Override
    protected String authorNameSelect() {
        return "div.article-meta >span";
    }


    @Override
    protected void setAuthorName(Document doc, Article article) {
        Element element = doc.select(authorNameSelect()).get(0);
        String authorName = text(element);
        if (StringUtils.isNotBlank(authorName)) {
            authorName = authorName.trim().replace("小编", "").replace("作者", "").replace("：", "").replace(":", "");
            article.setAuthorName(authorName);
        }
    }

    @Override
    protected void setPostDate(Document doc, Article article) {
        Element element = doc.select(dateSelect()).first();
        String time = text(element).replace(" ","");
        if (element != null) {
            Date date = DateUtil.parseDate(time);
            if (date != null) {
                article.setPostTime(date);
            }
        }

    }

    @Override
    protected String platform() {
        return "涨姿势";
    }
}
